package com.codejam.enums.demo.jackson;

import com.codejam.enums.demo.enums.IEnum;
import com.codejam.enums.demo.enums.WeekEnum;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.KeyDeserializer;
import java.io.IOException;
import lombok.SneakyThrows;

/**
 * 见本package下的readme.md文件
 *
 * @author zs
 * @date 2022年6月18日
 */
public class MapKeyWeekEnumDeserializer extends KeyDeserializer {
    @SneakyThrows
    @Override
    public IEnum deserializeKey(String key, DeserializationContext deserializationContext) throws IOException {

        WeekEnum[] values = WeekEnum.values();
        for (WeekEnum value : values) {
            //传code或传字母都认
            if (value.toString().equals(key) || (value.getCode() + "").equals(key)) {
                return value;
            }
        }
        String msg = String.format("枚举类型PerformanceTypeEnum从%s未能转换成功",key);
        throw new Exception(msg);
    }
}
